<template>
  <el-dialog
    :title="titleMap[mode]"
    v-model="visible"
    :width="500"
    destroy-on-close
    @closed="$emit('closed')"
  >
    <el-form
      :model="form"
      :rules="rules"
      :disabled="mode == 'show'"
      ref="dialogForm"
      label-width="100px"
    >
      <el-form-item label="上级单位" prop="fParentId">
        <!-- <el-tree-select
          v-model="form.fParentId"
          :data="deptOptions2"
          style="width: 100%"
          :props="dpProps"
          :render-after-expand="false"
        /> -->

        <treeselect
          v-model="form.fParentId"
          :options="deptOptions2"
          :normalizer="normalizer2"
          placeholder=""
          style="width: 100%"
        >
        </treeselect>
      </el-form-item>
      <el-form-item label="部门编号" prop="cDepartmentNo">
        <el-input
          v-model="form.cDepartmentNo"
          type="text"
          placeholder="请输入部门编号"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门名称" prop="cDepartmentName">
        <el-input
          v-model="form.cDepartmentName"
          type="text"
          placeholder="请输入部门名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="部门类型" prop="cTypeObj">
        <!-- <el-select
          v-model="form.cTypeObj"
          value-key="cName"
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option
            v-for="{ raw } in dict.type.bmlx"
            :key="raw.cName"
            :label="raw.cName"
            :value="raw"
          ></el-option>
        </el-select> -->

        <sc-select
          v-model="form.cTypeObj"
          dic="bmlx"
          :objValueType="true"
          valueKey="cValue"
          clearable
          filterable
          style="width: 100%"
        ></sc-select>
      </el-form-item>
      <el-form-item label="部门级别" prop="cLevelObj">
        <sc-select
          v-model="form.cLevelObj"
          dic="bmjb"
          clearable
          :objValueType="true"
          valueKey="cValue"
          filterable
          style="width: 100%"
        ></sc-select>
        <!-- <el-select
          v-model="form.cLevelObj"
          value-key="cName"
          placeholder="请选择"
          style="width: 100%"
        >
          <el-option
            v-for="{ raw } in dict.type.bmjb"
            :key="raw.cName"
            :label="raw.cName"
            :value="raw"
          ></el-option>
        </el-select> -->
      </el-form-item>
      <el-form-item label="区域国际编码" prop="cAreaCode">
        <el-input
          v-model="form.cAreaCode"
          placeholder="请输入区域国际编码"
        ></el-input>
        <div style="font-size: 12px" v-if="form.id">
          国际编码变更后下辖国际编码不会动态调整，请谨慎修改！！！
        </div>
      </el-form-item>

      <el-form-item label="排序值" prop="fSort">
        <el-input
          v-model="form.fSort"
          type="number"
          placeholder="请输入排序值"
        ></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false">取 消</el-button>
      <el-button
        v-if="mode != 'show'"
        type="primary"
        :loading="isSaveing"
        @click="submit()"
        >保 存</el-button
      >
    </template>
  </el-dialog>
</template>

<script>
export default {
  emits: ["success", "closed"],
  props: {
    deptOptions2: {
      type: Array,
      default: [],
    },
  },
  data() {
    return {
      mode: "add",
      dpProps: {
        label: "cDepartmentName",
        value: "id",
      },
      titleMap: {
        add: "新增",
        edit: "编辑",
        show: "查看",
      },
      visible: false,
      isSaveing: false,
      //表单数据
      form: {
        fParentId: 0,
      },
      //验证规则
      rules: {
        sort: [{ required: true, message: "请输入排序", trigger: "change" }],
        label: [{ required: true, message: "请输入部门名称" }],
      },
      //所需数据选项
      groups: [],
      groupsProps: {
        value: "id",
        emitPath: false,
        checkStrictly: true,
      },
    };
  },
  mounted() {
    // this.getGroup();
  },
  methods: {
    normalizer2(node) {
      // 去掉children=[]的children属性
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.id,
        label: node.cDepartmentName,
        children: node.children,
      };
    },
    //显示
    open(mode = "add") {
      this.mode = mode;
      this.visible = true;
      return this;
    },
    //加载树数据
    async getGroup() {
      var res = await this.$API.system.dept.list.get();
      this.groups = res.data;
    },
    //表单提交方法
    submit() {
      this.$refs.dialogForm.validate(async (valid) => {
        if (valid) {
          this.isSaveing = true;
          try {
            console.log(this.form);
            this.form.cTypeName = this.form.cTypeObj.cName;
            this.form.cTypeValue = this.form.cTypeObj.cValue;
            this.form.cLevelName = this.form.cLevelObj.cName;
            this.form.cLevelValue = this.form.cLevelObj.cValue;
            const params = {
              ...this.form,
              fDingOrganizationId: this.form.fDingOrganizationId || 0,
            };
            console.log(this.mode);

            await (this.mode === "add"
              ? this.$API.system.dept.AddDepartmentInfo.post(params)
              : this.$API.system.dept.UpdateDepartmentInfo.post(params));
            this.isSaveing = false;
            this.$emit("success", params);
            this.visible = false;
            this.$message.success("操作成功");
          } catch (error) {
            // this.visible = false;
            // this.$alert(error, "提示", { type: "error" });
          }
          // var res = await this.$API.demo.post.post(this.form);
          // this.isSaveing = false;
          // if (res.code == 200) {
          //   this.$emit("success", this.form, this.mode);
          //   this.visible = false;
          //   this.$message.success("操作成功");
          // } else {
          //   this.$alert(res.message, "提示", { type: "error" });
          // }
        }
      });
    },
    //表单注入数据
    setData(data) {
      // this.form.id = data.id;
      // this.form.label = data.label;
      // this.form.status = data.status;
      // this.form.sort = data.sort;
      // this.form.parentId = data.parentId;
      // this.form.remark = data.remark;

      //可以和上面一样单个注入，也可以像下面一样直接合并进去

      Object.assign(this.form, data);
      this.form.cTypeObj = {
        cName: data.cTypeName,
        cValue: data.cTypeValue,
      };
      this.form.cLevelObj = {
        cName: data.cTypeName,
        cValue: data.cTypeValue,
      };
    },
  },
};
</script>

<style></style>
